<template>
  <el-dialog title="选择项目"
             :visible.sync="showFlag"
             :modal="false"
             width="80%"
             :show-close="false"
             :close-on-click-modal="false"
             :fullscreen="device ==='mobile'"
  >
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="省份" prop="province">
        <el-select v-model="queryParams.province" clearable filterable placeholder="请选择省份" style="width: 100%;">
          <el-option v-for="item in provinceList" :key="item.code" :label="item.name" :value="item.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model.trim="queryParams.projectName" size="small" clearable :minlength="50"
                  placeholder="请输入项目名称"></el-input>
      </el-form-item>
      <el-form-item label="项目编号" prop="projectNo">
        <el-input v-model.trim="queryParams.projectNo" size="small" clearable :minlength="500"
                  placeholder="请输入项目编号"></el-input>
      </el-form-item>
      <el-form-item label="中标单位" prop="bidUnit">
        <el-input v-model.trim="queryParams.bidUnit" :maxlength="50" clearable placeholder="请填写中标单位名称"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="itemList" max-height="500" @current-change="handleCurrent" @row-dblclick="handleRowDbClick">
      <el-table-column width="50" align="center" label="选择" >
        <template v-slot="scope">
          <el-radio v-model="selectedItemId" :label="scope.row.id" @change="handleRowChange(scope.row)">{{""}}</el-radio>
        </template>
      </el-table-column>
      <el-table-column type="index" width="50" align="center" label="序号">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column property="province" label="省份" align="center" width="100"/>
      <el-table-column property="projectName" label="项目名称" align="center"/>
      <el-table-column property="projectNo" label="项目编号" align="center"/>
      <el-table-column property="orderSendDate" label="订单下发时间" align="center"/>
      <el-table-column property="orderFinishDate" label="订单完成时间" align="center"/>
      <el-table-column property="bidUnit" label="中标单位" align="center"/>
      <el-table-column property="address" label="收货地址" align="center"/>
      <el-table-column property="contactPerson" label="联系人" align="center"/>
      <el-table-column property="remark" label="备注" align="center"/>
      <el-table-column property="createBy" label="创建人" align="center" width="80"/>
      <el-table-column property="createTime" label="创建时间" align="center" sortable/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="handleCloseDialog">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDataListApi } from "@/api/crm/sa/task/delivery";
import { mapGetters } from 'vuex'
import ProvinceData from "@/views/crm/data/province.json";

export default {
  name: "OrderSelect",
  props: {
    showFlag: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 省份数据
      provinceList: [],
      // 选中数组
      selectedItemId: null,
      selectedRows: undefined,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料产品表格数据
      itemList: null,
      // 部门树选项
      itemTypeOptions: undefined,
      // 部门名称
      itemTypeName: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: "",
        projectNo: "",
        bidForm: ""
      },
    };
  },
  computed: {
    ...mapGetters([
      'device'
    ]),
  },
  watch: {
    // 根据名称筛选分类树
    itemTypeName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.provinceList = ProvinceData
    this.getList();
  },
  methods: {
    // 关闭弹窗
    handleCloseDialog() {
      this.$emit('update:showFlag', false);
    },
    // 获取根据list中的value返回label
    getListValue(data, list, value = 'value') {
      let label = ''
      list.map((item) => {
        if (+item[value] === +data) {
          label = item.label
        }
      })
      return label
    },
    /** 查询物料编码列表 */
    getList() {
      this.loading = true;
      getDataListApi(this.queryParams).then(response => {
          this.itemList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleCurrent(row){
      if(row){
        this.selectedRows = row;
      }
    },
    handleRowDbClick(row){
      if(row){
        this.selectedRows = row;
        this.$emit('onSelected',this.selectedRows);
        this.handleCloseDialog()
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if(row){
        this.selectedRows = row;
      }
    },
    //确定选中
    confirmSelect(){
      if(this.selectedItemId == null || this.selectedItemId == 0){
        this.$notify({
          title:'提示',
          type:'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.$emit('onSelected',this.selectedRows);
      this.handleCloseDialog()
    }
  }
};
</script>
